home *** CD-ROM | disk | FTP | other *** search
/ Introduction to 3D Game …ogramming with DirectX 12 / Introduction-to-3D-Game-Programming-with-DirectX-12.ISO / Code.Textures / Chapter 9 Texturing / Crate / Shaders / Default.hlsl next >
Encoding:
Text File  |  2016-03-02  |  3.4 KB  |  132 lines

  1. //***************************************************************************************
  2. // Default.hlsl by Frank Luna (C) 2015 All Rights Reserved.
  3. //
  4. // Default shader, currently supports lighting.
  5. //***************************************************************************************
  6.  
  7. // Defaults for number of lights.
  8. #ifndef NUM_DIR_LIGHTS
  9.     #define NUM_DIR_LIGHTS 3
  10. #endif
  11.  
  12. #ifndef NUM_POINT_LIGHTS
  13.     #define NUM_POINT_LIGHTS 0
  14. #endif
  15.  
  16. #ifndef NUM_SPOT_LIGHTS
  17.     #define NUM_SPOT_LIGHTS 0
  18. #endif
  19.  
  20. // Include structures and functions for lighting.
  21. #include "LightingUtil.hlsl"
  22.  
  23. Texture2D    gDiffuseMap : register(t0);
  24. SamplerState gsamLinear  : register(s0);
  25.  
  26.  
  27. // Constant data that varies per frame.
  28. cbuffer cbPerObject : register(b0)
  29. {
  30.     float4x4 gWorld;
  31.     float4x4 gTexTransform;
  32. };
  33.  
  34. // Constant data that varies per material.
  35. cbuffer cbPass : register(b1)
  36. {
  37.     float4x4 gView;
  38.     float4x4 gInvView;
  39.     float4x4 gProj;
  40.     float4x4 gInvProj;
  41.     float4x4 gViewProj;
  42.     float4x4 gInvViewProj;
  43.     float3 gEyePosW;
  44.     float cbPerObjectPad1;
  45.     float2 gRenderTargetSize;
  46.     float2 gInvRenderTargetSize;
  47.     float gNearZ;
  48.     float gFarZ;
  49.     float gTotalTime;
  50.     float gDeltaTime;
  51.     float4 gAmbientLight;
  52.  
  53.     // Indices [0, NUM_DIR_LIGHTS) are directional lights;
  54.     // indices [NUM_DIR_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHTS) are point lights;
  55.     // indices [NUM_DIR_LIGHTS+NUM_POINT_LIGHTS, NUM_DIR_LIGHTS+NUM_POINT_LIGHT+NUM_SPOT_LIGHTS)
  56.     // are spot lights for a maximum of MaxLights per object.
  57.     Light gLights[MaxLights];
  58. };
  59.  
  60. cbuffer cbMaterial : register(b2)
  61. {
  62.     float4 gDiffuseAlbedo;
  63.     float3 gFresnelR0;
  64.     float  gRoughness;
  65.     float4x4 gMatTransform;
  66. };
  67.  
  68. struct VertexIn
  69. {
  70.     float3 PosL    : POSITION;
  71.     float3 NormalL : NORMAL;
  72.     float2 TexC    : TEXCOORD;
  73. };
  74.  
  75. struct VertexOut
  76. {
  77.     float4 PosH    : SV_POSITION;
  78.     float3 PosW    : POSITION;
  79.     float3 NormalW : NORMAL;
  80.     float2 TexC    : TEXCOORD;
  81. };
  82.  
  83. VertexOut VS(VertexIn vin)
  84. {
  85.     VertexOut vout = (VertexOut)0.0f;
  86.     
  87.     // Transform to world space.
  88.     float4 posW = mul(float4(vin.PosL, 1.0f), gWorld);
  89.     vout.PosW = posW.xyz;
  90.  
  91.     // Assumes nonuniform scaling; otherwise, need to use inverse-transpose of world matrix.
  92.     vout.NormalW = mul(vin.NormalL, (float3x3)gWorld);
  93.  
  94.     // Transform to homogeneous clip space.
  95.     vout.PosH = mul(posW, gViewProj);
  96.     
  97.     // Output vertex attributes for interpolation across triangle.
  98.     float4 texC = mul(float4(vin.TexC, 0.0f, 1.0f), gTexTransform);
  99.     vout.TexC = mul(texC, gMatTransform).xy;
  100.  
  101.     return vout;
  102. }
  103.  
  104. float4 PS(VertexOut pin) : SV_Target
  105. {
  106.     float4 diffuseAlbedo = gDiffuseMap.Sample(gsamLinear, pin.TexC) * gDiffuseAlbedo;
  107.  
  108.     // Interpolating normal can unnormalize it, so renormalize it.
  109.     pin.NormalW = normalize(pin.NormalW);
  110.  
  111.     // Vector from point being lit to eye. 
  112.     float3 toEyeW = normalize(gEyePosW - pin.PosW);
  113.  
  114.     // Light terms.
  115.     float4 ambient = gAmbientLight*diffuseAlbedo;
  116.  
  117.     const float shininess = 1.0f - gRoughness;
  118.     Material mat = { diffuseAlbedo, gFresnelR0, shininess };
  119.     float3 shadowFactor = 1.0f;
  120.     float4 directLight = ComputeLighting(gLights, mat, pin.PosW,
  121.         pin.NormalW, toEyeW, shadowFactor);
  122.  
  123.     float4 litColor = ambient + directLight;
  124.  
  125.     // Common convention to take alpha from diffuse material.
  126.     litColor.a = diffuseAlbedo.a;
  127.  
  128.     return litColor;
  129. }
  130.  
  131.  
  132.